<?php
include("gen.php");
$cmd=get_datan("cmd");
switch($cmd){
    case 1:
        //get one community member based on id
        get_service();
        break;
    case 2:
        get_all_servoces();
        break;
    case 3:
        update_service();
        break;
    case 4:
        add_service();
        break;
    case 5:
        delete_service();
        break;
    case 6:
        search_services();
        break;
    default:
        echo "{";
        echo jsonn("result",0). ",";
        echo jsons("message","unknown command");
        echo "}";
}


function get_service(){
    include_once("service.php");

    $id=get_datan("id");
    $v=new service();
    $row=$v->get_service($id);
    if(!$row){
        echo "{";
        echo jsonn("result",0). ",";
        echo jsons("message","service not found");
        echo "}";
        return;
    }

    echo "{";
    echo jsonn("result",1) .",";
    echo '"service":{';
    echo jsonn("id",$id).",";

    echo jsons("service_id",$row['service_id']).",";
    //service offered
    echo jsons("service_offered",$row['service_offered']).",";
    //household
    echo jsons("household_id",$row['household_id']).",";
    //community_member_id
    echo jsonn("community_member_id",$row['community_member_id']).",";
    //date
    echo jsons("date_offered",$row['date_offered']);

    echo "}";
    echo "}";
}

function add_service()
{

    $service_offered = get_data('sf');;
    $household_id = get_data('hd');
    $community_member_id = get_data('cm');
    $date_offered = get_data('df');

    include("service.php");
    $v = new service();
    if(!$v-> add_service( $service_offered,$household_id, $community_member_id,
        $date_offered))
    {
        echo '{"result": 0,"message":"adding failed"}';
    }
    else
    {
        echo '{"result": 1,"message":"adding successful"}';
    }
}

function update_service(){

    $service_id=get_datan('sid');

    $service_offered=get_data('sof');

    $household_id=get_data('hsd');

    $community_member_id=get_datan('cid');

    $date_offered=get_data('dof');

    include_once("service.php");
    $v = new service();
    $row = $v->update_service($service_id,$service_offered,$household_id,$community_member_id,$date_offered);
    if(!$row){
        //return error message
        echo "{";
        echo jsonn("result",0). ",";
        echo jsons("message","id not correct");
        echo "}";
        return;
    }
}

    function delete_service()
    {
    $service_id=get_datan('sd');
        include_once("service.php");
        $v = new service();

        if(!$v->delete_services($service_id))
        {
            //return error message
            echo "{";
            echo jsonn("result",0). ",";
            echo jsons("message","id not correct");
            echo "}";
            return;
        }
    }
    
    function search_services(){
	include_once("service.php");
	$service_id=get_data('sid');
	$v = new service();
	$p=$v->search_services($service_id);
	$row=$v->fetch();
	
	 if(!$row){
		      //return error message
			  echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","id not correct");
			echo "}";
			return;			  
			  }
                          echo "{";
			echo jsonn("result",1) .",";
			echo '"service":[';			  
			  while($row){

			 echo '{';
			echo jsonn("service_id",$row['service_id']).",";

			echo jsons("service_offered",$row['service_offered']).",";

            //household_id
			echo jsonn("household_id",$row['household_id']).",";
			
			//communitymemberid
			echo jsonn("community_member_id",$row['community_member_id']).",";
			//date
			echo jsons("date_offered",$row['date_offered'])."}";
			
                        if($row=$v->fetch()){
                            echo ",";
                        }}
			echo "]";
		echo "}";
		 
	
    }
?>